<?php
/**
 * @desc 用户中心控制类
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class UserController extends Controller
{
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin',
        );
    }
	//个人中心首页
	public function actionIndex()
	{
		$request = Yii::app();
		$userInfo = json_decode($request->session['userinfo'], true);
		$currentPage = $request->request->getParam('current');
		$currentPage = empty($currentPage)?1:$currentPage;
		//获取用户信息
		$user = new User;
		$userData = $user->getUserInfo($userInfo['user_id']);
		$this->render('index', array(
				'userData' => $userData,
				'currentPage'=> $currentPage
			));
	}
	//用户设置修改
	public function actionModify(){
		$request = Yii::app();
		$userInfo = json_decode($request->session['userinfo'], true);
		$data = array('status' => 0, 'body' => array());
		if(!$request->request->isAjaxRequest){
			$data['status'] = 10002;
			$this->renderJson($data);
		}
		$method = $request->request->getParam('method');
		$model = new UserForm;
		switch($method){
			case "email":
				$val = $request->request->getParam('val');
				$result = $model->modifyEmail($userInfo['user_id'], $val);
			break;
			case "mobile":
				$val = $request->request->getParam('val');
				$result = $model->modifyMobile($userInfo['user_id'], $val);
			break;
			case "nick_name":
				$val = $request->request->getParam('val');
				$result = $model->modifyNickname($userInfo['user_id'], $val);
			break;
			case "change_passwd":
				$passwds['passwd'] = $request->request->getParam('passwd');
				$passwds['repasswd'] = $request->request->getParam('repasswd');
				$passwds['newpasswd'] = $request->request->getParam('newpasswd');
				$result = $model->modifyPassword($userInfo['user_id'], $passwds);
			break;
			default:
				$data['status'] = 10002;
				$this->renderJson($data);
				break;
		}
		if(!$result){
			$data['status'] = 10005;
			$this->renderJson($data);
		}
		$this->renderJson($data);
	}
	//订单详情页面
	public function actionOrderdetail()
	{
		$request = Yii::app();
		$userInfo = json_decode($request->session['userinfo'], true);

		$ordersn = $request->request->getParam('ordersn');
		if(empty($ordersn)){
			throw new ExceptionClass("订单号不能为空");
		}
		//获取主订单
		$order = new Order;
		$mainOrder = $order->getOrderMain($ordersn, $userInfo['user_id']);
		$this->render('orderdetail', array(
				'ordersn' => $ordersn,
				'mainOrder' => $mainOrder
			));
	}
}
